package com.ruoyi.system.service;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.domain.Stocklist;
import com.ruoyi.system.util.HtmlUtil;
import com.ruoyi.system.util.RedisCache;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InitService implements InitializingBean {

    @Autowired
    IStockService stockService;

    @Autowired
    IStocklistService stocklistService;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public HtmlUtil htmlUtil;

    @Override
    public void afterPropertiesSet() throws Exception {
        List<Stocklist> stocklists = stocklistService.selectStocklistList(new Stocklist());
        List<String> list = new ArrayList<>();
        for (Stocklist stocklist : stocklists) {
            if (!redisTemplate.hasKey(stocklist.getSymbol())) {
                list.add(stocklist.getSymbol());
            }
        }
        for (String key : list) {
            if (redisTemplate.hasKey(key)) {
               continue;
            }
            Stock stock = new Stock();
            stock.setStartTime(DateUtils.parseDate("2020-01-01"));
            stock.setSymbol(key);
            List<Stock> deail = stockService.selectStockList(stock);
            redisTemplate.opsForList().rightPushAll(key.substring(2), deail);
        }

//        Stock stock = new Stock();
//        stock.setStartTime(DateUtils.parseDate("2020-01-01"));
//        List<Stock> allDeail = stockService.selectStockList(stock);
//        RedisCache redisCache = new RedisCache();
//        Map<String, List<Stock>> collect = allDeail.stream().collect(Collectors.groupingBy(Stock::getSymbol));
//        for (String key : collect.keySet()) {
//            if (!redisTemplate.hasKey(key)) {
//                redisTemplate.opsForList().rightPushAll(key, collect.get(key));
//            }
//        }

        System.out.println("完成预热");
    }
}
